/*
 * @Author: Turump 
 * @Date: 2021-04-09 11:03:18 
 * @Last Modified by: Turump
 * @Last Modified time: 2021-05-25 18:49:25
 * 手指提示, 一次提示，加一次本组件
 * !!!使用方式: 在Canvas添加手指节点,然后将所有提示任务脚本都加于此节点
 */

import { TaskBase } from "./TaskBase";

const { ccclass, property, menu } = cc._decorator;

//添加类型,请直接在最后添加，不要扰乱顺序
export enum FingerTipType {
    SingleClick = 0,
    Move = 1
}

@ccclass("FingerTipNode")
export class FingerTipNode {
    @property({ displayName: "节点", type: cc.Node })
    node: cc.Node = null;

    @property({ displayName: "设置偏移量" })
    isOffset = false;

    @property({ displayName: "偏移坐标", visible() { return this.isOffset } })
    offset: cc.Vec2 = cc.v2(0, 0);
}

@ccclass
@menu("任务组件/手指操作提示")
export default class FingerTask extends TaskBase {
    @property({ displayName: "任务完成Emit事件", visible: false, override: true })
    doneEvent = ""
    @property({ displayName: "触发暂停的事件名", visible: false, override: true })
    pauseEventName = ""
    @property({ displayName: "触发恢复的事件名", visible: false, override: true })
    resumeEventName = ""
    @property({ displayName: "任务支持停止", override: true })
    isPauseable = false;

    @property({ type: cc.Enum(FingerTipType), displayName: "操作类型" })
    tipType: FingerTipType = FingerTipType.SingleClick;

    @property({ displayName: "提示次数" })
    repeatCount = 100000;

    @property({ displayName: "起始节点", type: FingerTipNode })
    startNode: FingerTipNode = new FingerTipNode;
    @property({ displayName: "目标节点", type: FingerTipNode, visible() { return this.tipType == FingerTipType.Move } })
    targetNode: FingerTipNode = new FingerTipNode;

    tweenAction: cc.Tween = null;

    onLoad() {
        super.onLoad();
        this.node.setAnchorPoint(cc.v2(0.05, 0.9));
        this.node.opacity = 0;
        this.node.zIndex = cc.macro.MAX_ZINDEX;
    }

    enableTask() {
        super.enableTask();
        this.runAction();
        this.node.zIndex = cc.macro.MAX_ZINDEX
    }

    stopTask() {
        super.stopTask();
        this.tweenAction && this.tweenAction.stop();
        this.enabled = false;
        this.node.opacity = 0;
    }

    private runAction() {
        if (!this.startNode || !this.startNode.node) {
            console.error(`${this.name} not set start node`)
            return;
        }
        let startPointWorld = this.startNode.node.convertToWorldSpaceAR(this.startNode.offset);
        let startPointLocal = this.node.parent.convertToNodeSpaceAR(startPointWorld);
        if (this.tipType == FingerTipType.SingleClick) {
            this.tweenAction = cc.tween(this.node).set({ position: cc.v3(startPointLocal), opacity: 255 })
                .repeat(this.repeatCount,
                    // cc.tween(this.node).by(0.5, { position: cc.v3(-10, 10) }).by(0.5, { position: cc.v3(10, -10) })
                    cc.tween(this.node).by(0.5, { scale: -0.1 }).by(0.5, { scale: 0.1 })
                ).start();
            return;
        }
        if (this.tipType == FingerTipType.Move) {
            if (!this.targetNode || !this.targetNode.node) {
                console.error(`${this.name} not set target node`)
                return;
            }
            let targetPointWorld = this.targetNode.node.convertToWorldSpaceAR(this.targetNode.offset);
            let targetPointLocal = this.node.parent.convertToNodeSpaceAR(targetPointWorld);
            this.tweenAction = cc.tween(this.node).set({ position: cc.v3(startPointLocal) })
                .repeat(this.repeatCount,
                    cc.tween(this.node).set({ opacity: 255 })
                        .to(1, { position: cc.v3(targetPointLocal) })
                        .set({ position: cc.v3(startPointLocal), opacity: 0 })
                        .delay(2)

                ).call(() => {
                    this.stopTask();
                    this.tweenAction = null;
                }).start();
            return;
        }
    }
}
